<template>
  <div class="app-container">
    <el-form
      class="formList"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-row>
        <el-form-item label="员工名称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入员工名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="选择日期" prop="sendDate">
          <el-date-picker v-model="queryParams.sendDate" type="daterange" align="right" value-format="yyyy-MM-dd"
            unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            >
          </el-date-picker>
        </el-form-item>

        <!-- <el-form-item label="状态" prop="dataStatus">
          <el-select
            v-model="queryParams.dataStatus"
            placeholder="请选择状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item> -->

        <el-form-item style="margin-left: 22px">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:brand:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:brand:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:brand:remove']"
          >删除</el-button
        >
      </el-col> -->

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table
        border
        v-loading="loading"
        :data="brandList"
        @selection-change="handleSelectionChange"
      >
        

        <el-table-column label="员工名称" align="center" prop="nickName" />
        <el-table-column label="所属门店" align="center" prop="storeName" />

        <el-table-column label="积分" align="center" prop="totalScore" />

        <el-table-column label="修改人" align="center" prop="createName" />

        <el-table-column
          label="修改时间"
          align="center"
          prop="createTime"
          width="180"
          sortable
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd(scope.row)"             
              >新增积分</el-button
            >

            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"             
              >积分明细</el-button
            >
            <el-button
              v-if="scope.row.dataStatus == '0'"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改员工/系列对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="750px"
      append-to-body
      @keyup.enter.native="submitForm"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="积分类型" prop="itemType">
          <el-radio
            v-model="form.itemType"
            label="add"
            @change="toGetPlanRoleList(form.itemType)"
            >增分项</el-radio
          >
          <el-radio
            v-model="form.itemType"
            label="reduce"
            @change="toGetPlanRoleList(form.itemType)"
            >减分项</el-radio
          >
        </el-form-item>

        <el-form-item label="积分项目" prop="itemId">
          <el-select
            v-model="form.itemId"
            placeholder="请选择"
            style="width: 120px"
            size="small"
          >
            <el-option
              v-for="item in postList"
              :key="item.itemId"
              :label="item.itemName"
              :value="item.itemId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数量" prop="number">
          <el-input
            v-model="form.number"
            placeholder="请输入数量"
            class="select-normal"
            maxlength="10"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="openA"
      width="750px"
      append-to-body
      @keyup.enter.native="submitForm"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-table
          border
          v-loading="loading"
          :data="brandListA"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />

          <el-table-column label="员工名称" align="center" prop="nickName" />
          <el-table-column label="积分项目" align="center" prop="itemName" />

          <el-table-column
            label="项目类别"
            align="center"
            prop="itemType"
          />
          <el-table-column label="积分变更" align="center" prop="score" />

          <el-table-column label="修改人" align="center" prop="createName" />

          <el-table-column
            label="修改时间"
            align="center"
            prop="createTime"
            width="180"
            sortable
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalA > 0"
          :total="totalA"
          :page.sync="queryParamsA.pageNum"
          :limit.sync="queryParamsA.pageSize"
          @pagination="handleUpdate(null)"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listScore,
  listDetail,
  delScore,
  addScore,
  updateScore,
  updateScoreStatus,
} from "@/api/service/scoreDetail";
import { listItem } from "@/api/service/score";
import imageArraySize from "@/components/ImageUpload/imageArraySize";
export default {
  name: "Score",
  components: {
    imageArraySize,
  },
  data() {
    return {
      technicainIdA: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中数组的名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总条数
      totalA: 0,
      // 员工/系列表格数据
      brandList: [],
      brandListA: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openA: false,
      // 用于类别关联的分类显示
      dialogVisible: false,
      //常用状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        dataStatus: null,
      },

      sendDate: null,
          sTime: null,
          eTime: null,
      // 查询参数
      queryParamsA: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        dataStatus: null,
      },
      type: "add",
      postList: [],

      // 表单参数
      form: {
        dialogImageUrl: "",
        dataPriority: "",
      },
      // 表单校验
      rules: {
        itemType: [
          {
            required: true,
            message: "积分类型不能为空",
            trigger: "blur",
          },
        ],
        itemId: [
          {
            required: true,
            message: "积分项目不能为空",
            trigger: "blur",
          },
        ],
        number: [
          {
            required: true,
            message: "数量不能为空",
            trigger: "blur",
          },
        ],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getList();

    this.getDicts("sys_data_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.toGetPlanRoleList();
  },
  methods: {
    dataPriorityInput(value) {
      let js = value + "";
      return js.replace(/^(0+)|[^\d]+/g, "");
    },
    /** 查询员工/系列列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.sendDate != null && this.queryParams.sendDate.length > 0) {
          let arr = this.queryParams.sendDate;
          this.queryParams.sTime = arr[0] + " 00:00:00";
          this.queryParams.eTime = arr[1] + " 23:59:59";
        }
      listScore(this.queryParams).then((response) => {
        this.brandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    async toGetPlanRoleList(value) {
      let res = await listItem({ itemType: value });
      if (res) {
        this.postList = res.rows;
      }
    },

    handleRemove(file, fileList) {},
    handlePictureCardPreviewLogo(file) {
      this.logoUr = file.url;
      this.dialogVisible = true;
    },
    handlePictureCardPreviewImg(file) {
      this.deviceImage = file.url;
      this.dialogVisible = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openA = false;
      this.reset();
    },
    // 关联的取消按钮
    cancelScore() {
      this.openScore = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemName: null,
        logoUrl: null,
        itemType: null,
        deviceImage: null,
        dataPriority: 10,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 员工菜单 */
    getMenuTreeselect() {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.itemId);
      this.names = selection.map((item) => item.itemName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      //用于禁用删除
      if (this.multiple == false) {
        for (var i = 0; i < selection.length; i++) {
          var element = selection[i];
          if ("1" === element.dataStatus) {
            this.multiple = true;
            break;
          }
        }
      }
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.dataStatus === "0" ? "无效" : "有效";
      this.$confirm(
        '确定要把员工名称为"' + row.itemName + '"设为' + text + "吗？",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return updateScoreStatus(row.itemId, row.dataStatus);
        })
        .then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        })
        .catch(function () {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.itemType = "add";
      this.form.technicainId = row.technicainId;
      this.toGetPlanRoleList(this.form.itemType);
      this.open = true;
      this.title = "添加员工积分";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if (row != null) {
        this.technicainIdA = row.technicainId;
        this.queryParamsA.pageNum =1 
        this.queryParamsA.pageSize =10 

      }
      console.log(row)
      console.log(this.technicainIdA)
      this.queryParamsA.technicainId = this.technicainIdA;
      listDetail(this.queryParamsA).then((response) => {
        this.brandListA = response.rows;
        this.totalA = response.total;
        this.openA = true;
        this.title = "员工积分明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addScore(this.form).then((response) => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      const names = row.itemName || this.names;
      this.$confirm('是否确认删除员工名称为"' + names + '"的员工?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delScore(itemIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/brand/export",
        {
          ...this.queryParams,
        },
        `商品员工.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.treeOne {
  display: inline-block;
  margin-right: 200px;
}

.treeTwo {
  display: inline-block;
}

.image-rows {
  width: 80px;
  height: auto;
}
</style>
